package svc

import (
	"context"
	"gozero-microservices/common-service/rpc/internal/config"
	"gozero-microservices/common-service/rpc/internal/model"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type ServiceContext struct {
	Config config.Config

	// 注入模型
	BannerModel model.MallBannerModel
	RegionModel model.MallRegionModel
	NavModel    model.MallNavModel
	Logger      logx.Logger
}

func NewServiceContext(c config.Config) *ServiceContext {
	// ------------- 获取mysql连接开始 -------------
	datasource := ""
	switch c.Env {
	case "dev":
		datasource = c.MysqlConf.Dev.DataSource
	case "test":
		datasource = c.MysqlConf.Test.DataSource
	case "prod":
		datasource = c.MysqlConf.Prod.DataSource
	}
	sc := sqlx.NewMysql(datasource)
	// ------------- 获取mysql连接结束 -------------

	return &ServiceContext{
		Config: c,

		// 创建模型
		BannerModel: model.NewMallBannerModel(sc),
		RegionModel: model.NewMallRegionModel(sc),
		NavModel:    model.NewMallNavModel(sc),
		Logger:      logx.WithContext(context.Background()),
	}
}
